package com.soufang.ui;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import com.zxx.common.Common;

import android.content.Intent; 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @brief 首页fragment页面
 * @author zxx
 *
 */
public class homeFragment extends Fragment {
	
	private TextView newHouseTextBtn;
	private Button chosecitybtn;
	//百度地图定位
	private LocationClient mLocClient;
	private MyLocationListenner myListener;
	private boolean isLoc = false;//是否成功定位
	private String mcity;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initloc();
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View contextView = inflater.inflate(R.layout.fragment_home, container, false);
		newHouseTextBtn = (TextView)contextView.findViewById(R.id.btnNewhouse);
		newHouseTextBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//把mcity作为附加参数发给新的页面，
				Bundle bundle= new Bundle();
				bundle.putSerializable(Common.KEY_CITY,mcity);
				Intent i = new Intent(getActivity(),newHouseActivity.class);
				i.putExtras(bundle);
				startActivity(i);
			}
		});
		
		chosecitybtn = (Button)contextView.findViewById(R.id.chosecity);
		
		return contextView;
	}

	private void initloc()
	{
		// 定位初始化
		mLocClient = new LocationClient(getActivity());
		myListener = new MyLocationListenner();
		mLocClient.registerLocationListener(myListener);
		LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);
		option.setOpenGps(true);
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(1000);
		option.setIsNeedAddress(true);
		mLocClient.setLocOption(option);
		mLocClient.start();
	}
	
	/**
	 * 定位SDK监听函数
	 */
	public class MyLocationListenner implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			// map view 销毁后不在处理新接收的位置
			if (location == null )
				return;
			if(!isLoc){
				StringBuffer sb = new StringBuffer(256);
				 sb.append("定位结束，经度 : ");
				 sb.append(location.getLatitude());
				 sb.append("\n纬度 : ");
				 sb.append(location.getLongitude());
				 sb.append("\n城市:");
				 mcity = location.getCity();
				 sb.append(mcity);
				 sb.append("\n地址:");
				 sb.append(location.getAddrStr());
				 if(mcity !=null && mcity.length() >0){
					 Toast.makeText(getActivity(), sb.toString(), Toast.LENGTH_LONG).show();
					 //选择城市按钮那变为福州市
					 chosecitybtn.setText(mcity);
					 isLoc = true;
				 }
				 else
				 {
					 return;
				 }
 
			}
			
		}

		public void onReceivePoi(BDLocation poiLocation) {
		}
	}
	

	@Override
	public void onDestroy() {
		// 退出时销毁定位
		mLocClient.stop();
		super.onDestroy();
	}
	
	
}
